package com.zrrd.blog.question.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zrrd.blog.api.entities.Label;
import com.zrrd.blog.api.entities.Question;
import com.zrrd.blog.api.entities.UserInfoREQ;
import com.zrrd.blog.api.feign.IFeignArticleController;
import com.zrrd.blog.question.mapper.QuestionMapper;
import com.zrrd.blog.question.req.QuestionUserREQ;
import com.zrrd.blog.question.service.IQuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.blog.util.base.BaseRequset;
import com.zrrd.blog.util.base.Result;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 问题信息表 服务实现类
 * </p>
 *
 * @author chen
 * @since 2023-08-07
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements IQuestionService {
    @Autowired
    private IFeignArticleController feignArticleController;

    @Override
    public Result findHotList(BaseRequset<Question> req) {
        QueryWrapper<Question> queryWrapper=new QueryWrapper<>();
        //select * from question where status in(1,2)
        queryWrapper.in("status", Arrays.asList(1,2));
        //order by reply desc
        queryWrapper.orderByDesc("reply");
        return Result.ok(baseMapper.selectPage(req.getPage(),queryWrapper));
    }

    @Override
    public Result findNewList(BaseRequset<Question> request) {
        QueryWrapper<Question> queryWrapper=new QueryWrapper<>();
        //select * from question where status in(1,2)
        queryWrapper.in("status", Arrays.asList(1,2));
        queryWrapper.eq("reply",0);
        //order by reply desc
        queryWrapper.orderByDesc("update_date");
        return Result.ok(baseMapper.selectPage(request.getPage(),queryWrapper));
    }

    @Override
    public Result findWaitList(BaseRequset<Question> request) {
        QueryWrapper<Question> queryWrapper=new QueryWrapper<>();
        //select * from question where status in(1,2)
        queryWrapper.in("status", 1);
        //order by reply desc
        queryWrapper.orderByDesc("update_date");
        return Result.ok(baseMapper.selectPage(request.getPage(),queryWrapper));
    }

    @Override
    public Result findListByLabelId(BaseRequset<Question> req, String labelId) {
        if(StringUtils.isEmpty(labelId)){
            return Result.error("标签id不能为空");
        }
        return Result.ok(baseMapper.findListByLabelId(req.getPage(),labelId));
    }

    @Override
    public Result findById(String id) {
        Question question=baseMapper.findQuestionAndLabelIdsById(id);
        if (question == null) {
            return Result.error("没有问题详情信息");
        }
        //TODO 远程方法调用article微服务获取Label信息
        if(CollectionUtils.isNotEmpty(question.getLabelIds())){
            List<Label> labelList=feignArticleController.getLabelListByIds(question.getLabelIds());
            question.setLabelList(labelList);
        }
        return Result.ok(question);
    }

    @Override
    public Result updateViewCount(String id) {
        if(StringUtils.isEmpty(id)){
            return Result.error("无效操作");
        }
        Question question=baseMapper.selectById(id);
        if(question==null){
            return Result.error("问题不存在");
        }
        question.setViewCount(question.getViewCount()+1);
        baseMapper.updateById(question);
        return Result.ok();
    }

    @Override
    public Result updateOrSave(Question question) {
        if(StringUtils.isNotEmpty(question.getId())){
            baseMapper.deleteQuestionLabel(question.getId());
            question.setUpdateDate(new Date());
        }
        super.saveOrUpdate(question);
        if(CollectionUtils.isNotEmpty(question.getLabelIds())){
            baseMapper.saveQuestionLabel(question.getId(),question.getLabelIds());
        }
        return Result.ok(question.getId());
    }

    @Override
    public Result updateStatus(String id, Integer status) {
        Question question=baseMapper.selectById(id);
        question.setStatus(status);
        question.setUpdateDate(new Date());
        baseMapper.updateById(question);
        return Result.ok();
    }

    @Override
    public Result deleteById(String id) {
        return this.updateStatus(id,0);
    }

    @Override
    public Result updateThumhup(String id, int count) {
        if(count!=-1 && count!=1){
            return Result.error("无效操作");
        }
        if(StringUtils.isEmpty(id)){
            return Result.error("无效操作");
        }
        Question question=baseMapper.selectById(id);
        if(question==null){
            return Result.error("问题不存在");
        }
        if(question.getThumhup()<=0 && count==-1){
            return Result.error("无效操作");
        }
        question.setThumhup(question.getThumhup()+count);
        baseMapper.updateById(question);
        return Result.ok();
    }

    @Override
    public Result findListByUserId(QuestionUserREQ req) {
        if(StringUtils.isEmpty(req.getUserId())){
            return Result.error("无效用户信息");
        }
        QueryWrapper<Question> wrapper=new QueryWrapper<>();
        wrapper.in("status",Arrays.asList(1,2));
        wrapper.eq("user_id",req.getUserId());
        wrapper.orderByDesc("update_date");

        return Result.ok(baseMapper.selectPage(req.getPage(),wrapper));
    }

    @Override
    public Result getQuestionTotal() {
        QueryWrapper<Question> wrapper=new QueryWrapper<>();
        wrapper.in("status",Arrays.asList(1,2));
        return Result.ok(baseMapper.selectCount(wrapper));
    }

    @Override
    public boolean updateUserInfo(UserInfoREQ req) {
        return baseMapper.updateUserInfo(req);
    }
}
